package com.example.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import com.example.demo.dto.ApplicationDTO;
import com.example.demo.dto.PageExtra;
import com.example.demo.request.CreateApplicationRequest;
import com.example.demo.response.ApiResponse;
import com.example.demo.service.ApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/applications")
//@Tag(name = "应用管理", description = "应用的增删改查")
public class ApplicationController {


    @Autowired
    private ApplicationService applicationService;

    //@Operation(summary = "获取应用列表", description = "支持分页")
    @GetMapping
    public ResponseEntity<ApiResponse<List<ApplicationDTO>>> getApplications(
            //@Parameter(description = "页码，从0开始")
             @RequestParam(defaultValue = "0") int page,
            //@Parameter(description = "每页数量")
             @RequestParam(defaultValue = "10") int size,
            HttpServletRequest request) {

        List<ApplicationDTO> applications = applicationService.getApplications(page, size);
        long total = applicationService.countApplications();
        PageExtra extra = new PageExtra(page, size, total);

        return ResponseEntity.ok(
                ApiResponse.success(applications, "请求成功", request.getRequestURI(), extra)
        );
    }

    //@Operation(summary = "获取应用详情")
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<ApplicationDTO>> getApplication(
            //@Parameter(description = "应用ID")
            @PathVariable Integer id,
            HttpServletRequest request) {

        ApplicationDTO application = applicationService.getApplicationById(id);
        return ResponseEntity.ok(
                ApiResponse.success(application, "查询成功", request.getRequestURI(), null)
        );
    }

    //@Operation(summary = "创建应用")
    @PostMapping
    public ResponseEntity<ApiResponse<ApplicationDTO>> createApplication(
            @Valid @RequestBody CreateApplicationRequest request,
            HttpServletRequest requestObj) {

        com.example.demo.entity.Application application = applicationService.createApplication(request);
        ApplicationDTO dto = ApplicationDTO.fromEntity(application);
        return ResponseEntity.status(201).body(
                ApiResponse.created(dto, "应用创建成功", requestObj.getRequestURI())
        );
    }

    //@Operation(summary = "更新应用")
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<ApplicationDTO>> updateApplication(
            @PathVariable Integer id,
            @Valid @RequestBody CreateApplicationRequest request,
            HttpServletRequest requestObj) {

        applicationService.updateApplication(id, request);
        ApplicationDTO application = applicationService.getApplicationById(id);
        return ResponseEntity.ok(
                ApiResponse.success(application, "应用更新成功", requestObj.getRequestURI(), null)
        );
    }

    //@Operation(summary = "删除应用")
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteApplication(
            @PathVariable Integer id,
            HttpServletRequest requestObj) {

        applicationService.deleteApplication(id);
        return ResponseEntity.ok(
                ApiResponse.success(null, "应用删除成功", requestObj.getRequestURI(), null)
        );
    }

    //@Operation(summary = "按名称搜索应用", description = "根据应用名称模糊搜索应用")
    @GetMapping("/search")
    public ResponseEntity<ApiResponse<List<ApplicationDTO>>> searchApplications(
            //@Parameter(description = "应用名称关键词")
            @RequestParam String name,
            HttpServletRequest request) {

        List<ApplicationDTO> applications = applicationService.searchApplicationsByName(name);
        return ResponseEntity.ok(
                ApiResponse.success(applications, "查询成功", request.getRequestURI(), null)
        );
    }
}